package com.samoilovich.belarustoday.basefragments;

import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.samoilovich.belarustoday.Constants;
import com.samoilovich.belarustoday.R;
import com.samoilovich.belarustoday.adapters.CategoryAdapter;
import com.samoilovich.belarustoday.datemodel.CategoryModel;
import com.samoilovich.belarustoday.tools.Utility;

import java.util.ArrayList;

public class NewsBaseFragment extends BaseFragment {
    private int mBackLabel;
    private static ArrayList<CategoryModel> sCategories;

    protected static int sTitle;
    protected int mNoImageId = 0;
    protected CategoryAdapter mAdapter;
    protected ListView mListView;
    protected ProgressBar mProgressBar;
    protected View mViewNoConnection;
    protected AdapterView.OnItemClickListener mOnItemClickListener;
    protected int mListItemResId;
    protected boolean mWasCreateNormal = false;

    public NewsBaseFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_main, null);
        mBackLabel = R.string.main_label;
        prepareViews();

        return mRootView;
    }


    @Override
    public void onResume() {
        super.onResume();
        if (sCategories != null) {
            updateScreen();
        }
    }

    @Override
    public void update(final ArrayList<CategoryModel> categories) {
        if (getActivity() != null) {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    sCategories = categories;
                    updateScreen();
                }
            });
        } else {
            sCategories = categories;
        }
    }

    @Override
    public void onReceiveInternetIntent(boolean hasInternetConnection, Intent intent) {
        if (mListView == null || mProgressBar == null || mViewNoConnection == null || mAdapter == null) {
            prepareViews();
        }
        if (intent.getAction().equals(Constants.PICTURE_LOADING_ACTION)) {
            if (mAdapter != null) {
                mAdapter.setPictureLoadingState(Utility.getPictureLoadingState(getActivity()));
            }
        } else {
            if (hasInternetConnection) {
                if (!mWasCreateNormal) {
                    if (sCategories == null) {
                        getCategories(getActivity());
                    }
                } else {
                    if (!mInProgress && sCategories == null) {
                        getCategories(getActivity());
                    }
                }
                mViewNoConnection.setVisibility(View.GONE);
            } else {
                if (sCategories == null) {
                    mViewNoConnection.setVisibility(View.VISIBLE);

                }
            }
        }
    }

    @Override
    public void updateAllData(Context context) {
        updateCategories();
        getCategories(context);
    }

    // -----------------------------------------------------------------------------------
    // -----------------------------------------------------------------------------------
    protected void prepareViews() {
        prepareActionBarViews(sTitle, mBackLabel, false);

        mListView = (ListView) mRootView.findViewById(R.id.listView_category);
        mProgressBar = (ProgressBar) mRootView.findViewById(R.id.progressBar_categoryWait);
        mViewNoConnection = mRootView.findViewById(R.id.view_noConnection);
        if (mNoImageId == 0) {
            mNoImageId = R.drawable.category_no_image;
        }
        mAdapter = new CategoryAdapter(getActivity(), null, mListItemResId, mNoImageId);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(mOnItemClickListener);
    }

    protected void getCategories(Context context) {
        sCategories = null;
    }

    protected void updateCategories() {
        sCategories = null;
        if (mListView == null || mProgressBar == null || mViewNoConnection == null || mAdapter == null) {
            prepareViews();
        }
        mListView.setVisibility(View.GONE);
        mProgressBar.setVisibility(View.VISIBLE);
        mViewNoConnection.setVisibility(View.GONE);
    }

    // -----------------------------------------------------------------------------------
    private void updateScreen() {
        if (mListView == null || mProgressBar == null || mViewNoConnection == null || mAdapter == null) {
            prepareViews();
        }
        mListView.setVisibility(View.VISIBLE);
        mProgressBar.setVisibility(View.GONE);
        mViewNoConnection.setVisibility(View.GONE);
        mAdapter.setCategories(sCategories);
    }
}
